/**
 *
 */
package ua.at.tsvetkov.samizdat.activities;

import java.io.File;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.adapters.AdtBooks;
import ua.at.tsvetkov.samizdat.data.Book;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.DataDownloader;
import ua.at.tsvetkov.samizdat.data.WrongFieldCountException;
import ua.at.tsvetkov.util.Log;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AcAuthorBooks extends AcTemplate {

	private static final int	DIALOG_ORDER_SELECT	= 3;
	private String					objectCode;
	private int						objectCount;
	private String					urlStr;
	private String					fileName;
	private String					authorName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		objectCode = getIntent().getExtras().getString("object");
		authorName = getIntent().getExtras().getString("author");
		try {
			objectCount = Integer.parseInt(getIntent().getExtras().getString("count"));
		} catch (NumberFormatException e) {
			Log.e("Неправильное количество", e);
			finish();
		}
		// http://samlib.ru/cgi-bin/areader?q=razdel&object=p/petrienko_p_w&order=title
		urlStr = Const.URL_RAZDEL + objectCode + "&order=data&page=1&pagelen=" + objectCount;

		String path = Const.PATH_BOOKS + authorName + File.separator;
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		fileName = path + ".list books.txt";
		dataDownloader = new DataDownloader(this, (ProgressDialog) onCreateDialog(DIALOG_LOAD), urlStr, Const.LIST_VIEW_COUNT);
		startPosition = 0;
		dataDownloader.setOnCompleteLoadListener(standartNotifier);
		addListView();
		dataDownloader.download(objectCount, fileName, true, false);
		adapter = new AdtBooks(this, dataDownloader, Const.LIST_VIEW_COUNT);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (showMore(position, listView))
					return;
				try {
					Book book = new Book(dataDownloader.getDataFields(position - 1, Data.BOOK_FIELDS_COUNT));
					book.openAcBook(AcAuthorBooks.this);
				} catch (WrongFieldCountException e) {
					e.printStackTrace();
				}
			}

		});
	}

	@Override
	protected void onResume() {
		adapter.refreshData();
		adapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			showDialog(DIALOG_ORDER_SELECT);
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return false;
	}

}
